package com.wenwu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsBasic;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsBasicMapper;
import com.wenwu.service.BasicService;

/**
 * @project RelicsProject-service
 * @class BasicServiceImpl.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月4日 下午11:34:49
 * @description 不可移动文物基本信息管理
 */
@Service
public class BasicServiceImpl implements BasicService {
	@Autowired
	private RelicsBasicMapper basicMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;

	public String addObj(RelicsBasic relicsBasic) throws RelicsException {
		int result = 0;
		try {
			List<RelicsAttachment> list = relicsBasic.getFileList();
			if (relicsBasic.getBasicId() == null) {
				result += basicMapper.insertSelective(relicsBasic);
				if (relicsBasic.getBasicId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("基本信息附件");
							attachment.setTableOwner(relicsBasic.getBasicId());// 外键
						}
						result += attachmentMapper.insertList(list);
					}
				}
				if (result > 0) {
					return "基本信息添加成功";
				} else {
					return "基本信息添加失败";
				}
			} else {
				result += attachmentMapper
						.deleteByKey(relicsBasic.getBasicId(),relicsBasic.getOwnList());// 先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("基本信息附件");
						attachment.setTableOwner(relicsBasic.getBasicId());// 外键
					}
					result += attachmentMapper.insertList(list);
				}
				result += basicMapper.updateByPrimaryKeySelective(relicsBasic);
				if (result > 0) {
					return "基本信息修改成功";
				} else {
					return "基本信息修改失败";
				}
			}
		} catch (Exception e) {
			throw new RelicsException("系统错误，操作失败");
		}
	}

	public int selectTotalRecord(RelicsBasic relicsBasic)
			throws RelicsException {
		try {
			return basicMapper.selectTotalRecord(relicsBasic);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public List<RelicsBasic> selectList(RelicsBasic relicsBasic,
			PaginationVO pageVo) throws RelicsException {
		try {
			return basicMapper.selectList(relicsBasic, pageVo);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public RelicsBasic selectObjByKey(Integer basicId) throws RelicsException {
		try {
			return basicMapper.selectByPrimaryKey(basicId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public int deleteByKey(List<?> keys) throws RelicsException {
		try {
			return basicMapper.deleteByKey(keys);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
}
